package com.c.ctgproject.ui.collect

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.BatteryCanHandlerAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityBatteryCanHandlerBinding
import com.c.ctgproject.entity.guang.BatteryCanHandlerEntity
import com.c.ctgproject.entity.guang.BatteryInWarehouseEntity
import com.c.ctgproject.entity.guang.CarCanHandlerEntity
import com.c.ctgproject.event.PositionsEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import org.greenrobot.eventbus.Subscribe
import org.jetbrains.anko.startActivity

/**
 *作者:LiBW
 *创建日期: 2021/9/9
 *描述: 新建拆解单 页面(电池) 新建出库单 可拆解电池 页面 可拆电池列表  电池出库复用
 */
class

BatteryCanHandlerActivity : BaseActivityV2(true) ,BatteryCanHandlerAdapter.OnCheckBoxClickedListener{
    private val mBinding by lazy { ActivityBatteryCanHandlerBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private var page = 1
    private var search: String = ""
    private val mList = ArrayList<BatteryCanHandlerEntity.DataBean.ListBean>()
    private var mAdapter: BatteryCanHandlerAdapter? = null
    private var chai = false
    private val name by lazy { intent.getStringExtra(MyConstants.TITLE_NAME) }

    @Subscribe
    fun event(type: PositionsEvent) {
        val removeList = ArrayList<BatteryCanHandlerEntity.DataBean.ListBean>()
        type.list.forEach { a ->
            mList.forEach { b ->
                if (a == b.pbId) {
                    removeList.add(b)
                }
            }
        }
        removeList.forEach {
            mList.remove(it)
        }

        mAdapter?.notifyDataSetChanged()
    }

    override fun event() {

        if (name == "新建出库单") {
            mBinding.mTabLayout.visibility = View.GONE
            mAdapter = BatteryCanHandlerAdapter(this, mList, true,this)
            mBinding.mTitleBar.setTitle("新建出库单")
            mBinding.mConfirmBTN.text = "去出库"
            chai = false
            val l = mBinding.mTabLayout.newTab()
            l.text = "未出库"
            mBinding.mTabLayout.addTab(l)
            val r = mBinding.mTabLayout.newTab()
            r.text = "我的"
            mBinding.mTabLayout.addTab(r)

        } else if (name == "新建拆解单") {
            mAdapter = BatteryCanHandlerAdapter(this, mList, false,this)
            chai = true
            mBinding.mTitleBar.setTitle("新建拆解单")
            val l = mBinding.mTabLayout.newTab()
            l.text = "未拆解"
            mBinding.mTabLayout.addTab(l)
            val r = mBinding.mTabLayout.newTab()
            r.text = "我的"
            mBinding.mTabLayout.addTab(r)
        }
        mBinding.mRecyclerView.adapter = mAdapter
        mBinding.mRecyclerView.layoutManager = LinearLayoutManager(this)

        mBinding.mSearchET.handlerText {
            if (it.isEmpty() || it == "") {
                search = ""
                page = 1
                getData()
            }
        }
        mBinding.mSearchTV.setOnClickListener {
            search = mBinding.mSearchET.text.handlerNull()
            page = 1
            getData()
        }
        mBinding.mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mBinding.mRefresh.setOnRefreshListener {
            page = 1
            getData()
        }

        getData()
    }


    private var searchMine = false
    override fun click() {
        mBinding.mTabLayout.handlerSelectReturnText {
            when (it) {
                "未出库", "未拆解" -> {
                    mBinding.mPartsTV.visibility = View.GONE
                    mBinding.mBottomRL.visibility = View.VISIBLE
                    searchMine = false
                    page = 1
                    getData()
                }
                "我的" -> {
                    mBinding.mPartsTV.visibility = View.GONE
                    mBinding.mBottomRL.visibility = View.GONE
                    searchMine = true
                    page = 1
                    getData()
                }
            }
        }
        //去拆解 和去出库
        mBinding.mConfirmBTN.setOnClickListener {
            val selectList = ArrayList<BatteryCanHandlerEntity.DataBean.ListBean>()
            mList.forEach {
                if (it.isSelect) selectList.add(it)
            }
            if (selectList.isEmpty()) {
                showToast("请至少选择一个进行拆解")
            } else {
                if (mBinding.mConfirmBTN.text == "去出库") {
                    //去出库
                    startActivity<BatteryOutSubmitActivity>(MyConstants.DATA to selectList)
                } else {
                    //去拆解
                    startActivity<BatteryHandlerSelectDataActivity>(MyConstants.DATA to selectList)
                }
            }
        }
    }

    private fun getData() {
        mBinding.mConfirmBTN.text="拆解"
        if (searchMine) {
            if (name == "新建出库单") {
                ApiMethods.apiSubscribe(ApiMethods.retrofit.myPowerBattery(search, page, batteryStatus = 3), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                    MyLog.d("我操作过的电池:$it")
                    if (page == 1) mList.clear()
                    val data = getGson().fromJson(it, BatteryCanHandlerEntity::class.java).data
                    data?.list?.let { list ->
                        mList.addAll(list)
                        mAdapter?.notifyDataSetChanged()
                        mBinding.mRecyclerView.handlerPosition(page)
                    }
                })


            } else if (name == "新建拆解单") {
                ApiMethods.apiSubscribe(ApiMethods.retrofit.myPowerBattery(search, page, dismantle = null), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                    MyLog.d("我操作过的电池:$it")
                    if (page == 1) mList.clear()
                    val data = getGson().fromJson(it, BatteryCanHandlerEntity::class.java).data
                    data?.list?.let { list ->
                        mList.addAll(list)
                        mAdapter?.notifyDataSetChanged()
                        mBinding.mRecyclerView.handlerPosition(page)
                    }
                })
            }
        } else {
            //        ApiMethods.apiSubscribe(ApiMethods.retrofit.powerBatteryList(1, search, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
            if (chai) {
                ApiMethods.apiSubscribe(ApiMethods.retrofit.batteryCanHandler(search, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                    MyLog.d("电池回调:$it")
                    val data = getGson().fromJson(it, BatteryCanHandlerEntity::class.java).data
                    data?.list?.let { list ->
                        if (page == 1) mList.clear()
                        mList.addAll(list)
                        mAdapter?.notifyDataSetChanged()
                        mBinding.mRecyclerView.handlerPosition(page)
                    }
                })
            } else {
//            ApiMethods.apiSubscribe(ApiMethods.retrofit.powerBatteryList(1, search, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
//            ApiMethods.apiSubscribe(ApiMethods.retrofit.batteryCanHandler( search, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                ApiMethods.apiSubscribe(ApiMethods.retrofit.batteryCanHandlerV2(search, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                    MyLog.d("电池回调:$it")
                    val data = getGson().fromJson(it, BatteryCanHandlerEntity::class.java)
                    data.outable?.let { bean ->
                        mBinding.mPartsTV.visibility = View.VISIBLE
                        val text = "可出库:${bean.packCount}个电池包,${bean.moduleCount}个模组,${bean.coreCount}个电芯"
                        mBinding.mPartsTV.text = text
                    }

                    data?.data?.list?.let { list ->
                        if (page == 1) mList.clear()
                        mList.addAll(list)
                        mAdapter?.notifyDataSetChanged()
                        mBinding.mRecyclerView.handlerPosition(page)
                    }
                })
            }
        }


    }

    override fun OnItemCheckBoxClicked() {
        val selectList = ArrayList<BatteryCanHandlerEntity.DataBean.ListBean>()
        mList.forEach {
            if (it.isSelect) selectList.add(it)
        }
        if(selectList.isEmpty()){
            mBinding.mConfirmBTN.text="拆解"
        }else{
            mBinding.mConfirmBTN.text="拆解(${selectList.size})"
        }
    }


}